<template>
  <div>
    <!-- 产品信息搜索 -->
    <TopFilter
      :list="topFilterInit"
      :cols="3"
      :collapse="collapse"
      labelWidth="90"
      @filterChange="changeHandle"
    ></TopFilter>
    <!-- table -->
    <FilterTable
      height="250"
      ref="listModal"
      :columns="columns"
      :selectionType="selectType"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      :onRowSelectChange="selectChange"
      columnsRef="cms-common_product_index_01"       
    ></FilterTable>
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">{{submit}}</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getBrand, getSeries, getModel, getPackage, getProduct, toSelectData } from '@/api/cms-common/basedata';
export default {
  mixins: [dictionary],
  props: {
    submit: {
      type: String,
      default: '确定'
    },
    selectType:{
      type: String,
      default: 'multiple'
    }
  },
  data() {
    return {
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi: getProduct,
      fetchParams: {}, //ipa 接口
      selectedRows: [] //选中物料
    };
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brandId',
          placeholder: '请输入',
          initialValue: '',
          itemList: [],
          change: val => {
            this.loadSeries(val);
            this.loadModel(null);
          }
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesId',
          placeholder: '请输入',
          initialValue: '',
          itemList: [],
          change: val => {
            this.loadModel(val);
          }
        },
        { type: 'SELECT', label: '车型', fieldName: 'modelId', placeholder: '请输入', initialValue: '', itemList: [] },
        { type: 'INPUT', label: '配置', fieldName: 'configNameCompoun', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '内饰', fieldName: 'interiorColorStr', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '颜色', fieldName: 'colorName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '整车编码', fieldName: 'productCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '产品名称', fieldName: 'productName', placeholder: '请输入', initialValue: '' }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({},val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '整车编号', dataIndex: 'productCode', sorter: true },
        { title: '产品名称', dataIndex: 'productName', sorter: true },
        { title: '品牌', dataIndex: 'brandName', sorter: true },
        { title: '车系', dataIndex: 'seriesName', sorter: true },
        { title: '车型', dataIndex: 'modelName', sorter: true },
        { title: '配置', dataIndex: 'packageName', sorter: true },
        { title: '内饰', dataIndex: 'trimColor', sorter: true },
        { title: '颜色', dataIndex: 'color', sorter: true }
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
    },
    //保存
    saveDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows });
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false});
    },
    //加载品牌
    async loadBrand() {
      let b = await getBrand({});
      this.topFilterInit.find(item => item.fieldName==='brandId').itemList = toSelectData(b.data, 'brandId', 'brandName');
    },
    //根据车系加载车型
    async loadModel(seriesId) {
      this.topFilterInit.find(item => item.fieldName==='modelId').initialValue = '';
      this.topFilterInit.find(item => item.fieldName==='modelId').itemList = [];
      if(!seriesId || typeof(seriesId)=='undefined' || seriesId=='')
        return;
      let brandId = this.topFilterInit.find(item => item.fieldName==='brandId').initialValue;
      let m = await getModel({ brandId: brandId, seriesId: seriesId });
      this.topFilterInit.find(item => item.fieldName==='modelId').itemList = toSelectData(m.data, 'modelId', 'modelName');
    },
    //根据品牌加载车系
    async loadSeries(brandId) {
      this.topFilterInit.find(item => item.fieldName==='seriesId').initialValue = '';
      this.topFilterInit.find(item => item.fieldName==='seriesId').itemList = [];
      if(!brandId || typeof(brandId)=='undefined' || brandId=='')
        return;
      let s = await getSeries({ brandId: brandId });
      this.topFilterInit.find(item => item.fieldName==='seriesId').itemList = toSelectData(s.data, 'seriesId', 'seriesName');
    }
  },
  mounted() {
    this.loadBrand();
    //this.BaseTable = this.$refs.listModal.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
